// components/dict-picker/index.js
import api from '../../api/index'
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    range:{ 
      type: Array,
      value: []
    },
    dictType: {
      type: [String, Array]
    },
    itemName: {
      type: String,
      value: 'name'
    },
    itemValue: {
      type: String,
      value: 'val'
    },
    value: {
      type: [String,Array],
      value: ''
    },
    disabled: {
      type: Boolean,
      value: false
    },
    suffix: {
      type: String,
      value: ''
    },
    text: {
      type: String,
      value: '请选择'
    },
    multiple: {
      type: Boolean,
      value: false
    },
    length: {
      type: Number,
      value: 15
    },
    columnsPlaceholder: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    indicatorStyle: `height: ${Math.round(app.globalData.screenHeight/(750/40))}px;`,
    index: [],
    inxs: [],
    type: '',
    texts: []
  },
  ready: function(){
    let type = 'select'
    if(this.data.multiple){

      type = 'multiple'
    }else{
      if(this.data.dictType instanceof Array || (!wx.$isNull(this.data.range) && this.data.range[0] instanceof Array)){
        type = 'selects'
      }
    }
    this.setData({
      type: type
    })

    if(this.data.type == 'selects'){
      if(!wx.$isNull(this.data.dictType)){
        for(const r in this.data.dictType){
          api.common.getDictData(this.data.dictType[r]).then(res=>{
            let range = []
            if(this.data.columnsPlaceholder != ''){
              range.push({ name: this.data.columnsPlaceholder, val:'' })
            }
            for(const i in res){
              range.push({name: res[i].dictLabel, val: res[i].dictValue })
            }

            this.initData(range, r)
          })
        }
      }else{
        for(const r in this.data.range){
          let range = []
          if(this.data.columnsPlaceholder != ''){
            range.push({name: this.data.columnsPlaceholder, val:''})
          }
          for(const i in this.data.range[r]){
            if(this.data.range[r][i] instanceof Object){
              range.push({name: this.data.range[r][i][this.data.itemName], val: this.data.range[r][i][this.data.itemValue]})
            }else{
              range.push({name: this.data.range[r][i], val: this.data.range[r][i]})
            }      
          }
          this.initData(range, r)
        }
      }

    } else {

      if(!wx.$isNull(this.data.dictType)){
        api.common.getDictData(this.data.dictType).then(res=>{
          let range = []
          if(this.data.columnsPlaceholder != ''){
            range.push({name: this.data.columnsPlaceholder, val:''})
          }
          for(const i in res){
            range.push({name: res[i].dictLabel, val: res[i].dictValue })
          }
          this.initData(range)
        })
      }else{
        let range = []
        if(this.data.columnsPlaceholder != ''){
          range.push({name: this.data.columnsPlaceholder, val:''})
        }
        for(const i in this.data.range){

          if(this.data.range[i] instanceof Object){
            range.push({name: this.data.range[i][this.data.itemName], val: this.data.range[i][this.data.itemValue]})
          }else{
            range.push({name: this.data.range[i], val: this.data.range[i]})
          }
        }
        this.initData(range)
      }
    }

  },
  /**
   * 组件的方法列表
   */
  methods: {
    showModal(e){
      this.setData({
        show: 'show'
      })
    },
    hideModal(e){
      this.setData({
        show: ''
      })
    },
    initData(data, index){
      let idx =[], oidx =[],text = '',texts = [] 
      if(this.data.type == 'select'){
        this.setData({
          range: data
        })
        if(wx.$isNull(this.data.value)){
          idx[0] = 0
          if(this.data.value==''){
            if(!wx.$isNull(this.data.columnsPlaceholder)){
              text = this.data.columnsPlaceholder
            }
          }
        }else {
          for(let i in data){
            if(this.data.value == data[i][this.data.itemValue]){
              idx[0] = parseInt(i)
              text = data[i][this.data.itemName] + ' ' +this.data.suffix
              break
            }
          }
        }

      }else if(this.data.type == 'selects'){
        let ranges = this.data.range, indexs = this.data.index, texts = this.data.texts
        ranges[index] = data
        this.setData({
          range: ranges
        })
        if(wx.$isNull(this.data.value) || wx.$isNull(this.data.value[index])){
          indexs[index] = 0
          idx = indexs
          oidx = indexs
          if(this.data.value instanceof Array && this.data.value[index] == ''){
            texts[index] = data[0][this.data.itemName]
            text = this.handleTxt(texts)
          }
        }else {
          for(const i in data){
            if(this.data.value[index] == data[i][this.data.itemValue]){
              indexs[index] = parseInt(i)
              texts[index] = data[i][this.data.itemName]
              text = this.handleTxt(texts)
              idx = indexs
              oidx = indexs

              break
            }
          }
        }

      }else if(this.data.type == 'multiple'){
        let indexs = []
        if(this.data.value == ''){
          for(const i in data){
            data[i].checked = false
          }
          data[0].checked = true
          indexs.push(0)
          texts.push(data[0][this.data.itemName])
        }else{
          for(const i in data){
            data[i].checked = false
            for(const v in this.data.value){
              if(this.data.value[v] == data[i][this.data.itemValue]){
                data[i].checked = true
                indexs.push(parseInt(i))
                texts.push(data[i][this.data.itemName])
                break
              }
            }
          }
        }


        text = this.handleTxt(texts)
        if(texts.length>0){
          idx[0] = indexs[0]
        }
        this.setData({
          range: data
        })
      }
      
      if(wx.$isNull(text)){
        text = '请选择'
      }

      let timer = setTimeout(() => { 
        this.setData({
          index: idx,
          inxs: oidx,
          text: text,
          texts: texts
        })
        clearTimeout(timer)
      },0)
    },
    onCheck(e){
      let d = e.currentTarget.dataset.src
      let selectV = d[this.data.itemValue]
      let r = this.data.range
      if(wx.$isNull(selectV)){
        // d checked cong false 转为true 这里是转之前
        if(d.checked){
          // 取消选中
          r[0].checked = false
        }else{
          // 选中
          r[0].checked = true
          for(const i in r){
            if(r[i][this.data.itemValue]){
              r[i].checked = false
            }
          }
        }
        this.setData({
          range: r
        })
      }else{
        for(const i in r){
          if(i == 0 ){
            r[i].checked = false
          }
          if(r[i][this.data.itemValue] == selectV){
            r[i].checked = !r[i].checked
            this.setData({
              range: r
            })
            break
          }
        }
      }
    },

    change(e){
      if(this.data.type == 'select'){
        const index = e.detail.value
        let val = this.data.range[index][this.data.itemValue]
        this.setData({
          index: index,
          value: val
        })
      } else if(this.data.type == 'selects'){
        let indexs = e.detail.value, v = [], t = []
        for(const i in indexs){
          const obj = this.data.range[i][indexs[i]]
          v.push(obj[this.data.itemValue])
          t.push(obj[this.data.itemName])
        }

        this.setData({
          texts: t,
          index: indexs,
          value: v
        })
      }
      
    },
    changeEnd(e){
      if(this.data.type == 'selects'){
        const oidx = this.data.inxs
        const nidx = this.data.index
        let isOne = true
        if(oidx[0]==nidx[0]){
          isOne = false
        }
        let v = this.data.value, t = this.data.texts
        let o1 = v[0], o2=v[1]
        if(isOne){
          if(o1>o2){
            for(const i in this.data.range[1]){
              if(o1<=this.data.range[1][i][this.data.itemValue]){
                nidx[1] = i
                v[1] = this.data.range[1][i][this.data.itemValue]
                t[1] = this.data.range[1][i][this.data.itemName]
                break
              }
            }
          }

        }else{
          if(o2<o1 && !wx.$isNull(o2)){
            for(const i in this.data.range[0]){
              if(this.data.range[0][i][this.data.itemValue]>=v[1]){
                let d,ii=0
                if(i>0){
                  ii = i-1
                }
                d = this.data.range[0][ii]
                nidx[0] = ii
                v[0] = d[this.data.itemValue]
                t[0] = d[this.data.itemName]
                break
              }
            }
          }
        }
        setTimeout(() => { 
          this.setData({
            index: nidx,
            inxs: nidx,
            value: v,
            texts: t
          })
        })
      }
    },
    okModal(e){
      let txt = ''
      let inx = this.data.index
      if(inx.length == 0){
        inx.push(0)
      }
      if(this.data.type == 'select'){
        this.setData({
          value: this.data.range[this.data.index].val
        })
        txt = this.handleTxt(this.data.range[this.data.index][this.data.itemName])
      }else if(this.data.type == 'selects'){
        txt = this.handleTxt(this.data.texts)
        if(wx.$isNull(this.data.value)){
          this.setData({
            value: ["",""]
          })
        }
        if(wx.$isNull(txt)){
          if(wx.$isNull(this.data.columnsPlaceholder)){
            txt = '请选择'
          }else{
            txt = this.data.columnsPlaceholder
          }
        }
      }else if(this.data.type == 'multiple'){
        let val =[],txts = []
        for(const i in this.data.range){
          if(this.data.range[i].checked){
            val.push(this.data.range[i][this.data.itemValue])
            txts.push(this.data.range[i][this.data.itemName])
          }
        }
        txt = this.handleTxt(txts)
        if(wx.$isNull(txt)){
          txt = '请选择'
        }
        this.setData({
          value: val
        })
      }
      
      this.setData({
        text: txt,
        show: ''
      })
      let d = {
        val: this.data.value,
        txt: txt
      }
      this.triggerEvent('change', d)
    },
    handleTxt(str){
      if(this.data.type == 'select'){
          return this.subText(str + ' ' + this.data.suffix)
      }else if(this.data.type == 'multiple'){
        let t = ''
        for(const i in str){
          if(t=='') {
            t = str[i] + this.data.suffix
          } else {
            t = t + ','+ str[i] + this.data.suffix
          }
        }
        return this.subText(t)

      }else if(this.data.type == 'selects'){
        if(this.data.value instanceof Array  && this.data.value[0] == '' && this.data.value[1] == ''){
          if(!wx.$isNull(this.data.columnsPlaceholder)){
            return this.data.columnsPlaceholder
          }
        }

        let t = ''
        for(const i in str){
          if(t=='') {
            t = str[i]
          } else {
            t = t + ' - '+ str[i] + this.data.suffix
          }
        }
        return this.subText(t)
      }
    },
    subText(str){
      if(str.length>this.data.length){
        return str.substring(0, this.data.length) + '... '
      }else{
        return str
      }
    },
  }
})
